<template>
  <page-header-wrapper>
    <page-tab-list>
      <div class="table-page-search-box">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="8" :sm="24">
              <a-form-item label="等级名称">
                <div class="flex">
                  <a-input allow-clear placeholder="请输入等级名称" v-model="queryParam.name"></a-input>
                  <a-button type="primary" @click="getList()">查询</a-button>
                </div>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </page-tab-list>

    <a-card :border="false">
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <div></div>

        <a-button type="primary" @click="visible = true">+ 新增</a-button>
      </div>

      <a-table
        style="width: 100%; overflow-x: auto"
        size="default"
        :columns="columns"
        :data-source="list"
        :row-key="(record) => record.id"
        :pagination="{
          current: queryParam.pageNo,
          total: this.total,
          pageSize: this.pageSize,
          showSizeChanger: false,
          onChange: this.tablePageChange,
        }"
      >
        <span slot="price" slot-scope="text"> 累计完成订单金额满{{ text }} </span>

        <span slot="status" slot-scope="text, record">
          <a-switch
            checked-children="开"
            un-checked-children="关"
            :checked="text === 1"
            @click="handleItem('status', record, text === 1 ? 0 : 1)"
          ></a-switch>
        </span>

        <span slot="action" slot-scope="text, record">
          <a-button type="link" size="small" @click="handleItem('edit', record)">编辑</a-button>
          <a-button type="link" size="small" @click="handleItem('status', record, 2)">删除</a-button>
        </span>
      </a-table>
    </a-card>

    <a-modal
      :title="form.id ? '修改' : '新增'"
      width="800px"
      v-model="visible"
      @ok="submit"
      :afterClose="closeModal"
      :destroyOnClose="true"
      :confirmLoading="btnLoading"
    >
      <a-form-model ref="ruleForm" :model="form" :rules="rules" :label-col="{ span: 4 }" :wrapper-col="{ span: 18 }">
        <a-form-model-item label="会员等级" prop="level">
          <a-select v-model="form.level" placeholder="请选择会员等级">
            <a-select-option v-for="item in 100" :key="item">
              {{ item }}
            </a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="等级名称" prop="name">
          <a-input v-model="form.name" placeholder="请填写等级名称" />
        </a-form-model-item>
        <a-form-model-item label="等级条件" prop="price">
          <div class="flexBox">
            <em>累计完成订单金额满</em>
            <a-input-number v-model="form.price" :min="0" :precision="2"></a-input-number>
            <span>元</span>
          </div>
        </a-form-model-item>
        <a-form-model-item label="折扣" prop="profit">
          <div class="flexBox">
            <a-input-number v-model="form.profit" :min="0" :max="10" :precision="1"></a-input-number>
            <span>折</span>
          </div>
        </a-form-model-item>
        <a-form-model-item label="会员等级状态" prop="status">
          <a-radio-group v-model="form.status">
            <a-radio value="1"> 开启</a-radio>
            <a-radio value="0"> 关闭</a-radio>
          </a-radio-group>
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </page-header-wrapper>
</template>

<script>
import { userLevelSearch, setUserLevelStatus, setUserLevel } from '@/api/auth'

const columns = [
  {
    title: '等级',
    dataIndex: 'level',
    width: 80,
  },
  {
    title: '等级名称',
    dataIndex: 'name',
  },
  {
    title: '折扣',
    dataIndex: 'profit',
  },
  {
    title: '升级条件',
    dataIndex: 'price',
    scopedSlots: {
      customRender: 'price',
    },
  },
  {
    title: '状态',
    dataIndex: 'status',
    scopedSlots: {
      customRender: 'status',
    },
  },
  {
    title: '操作',
    scopedSlots: {
      customRender: 'action',
    },
    width: 150,
  },
]

export default {
  data() {
    return {
      total: 0,
      pageNo: 1,
      pageSize: 10,
      columns,
      queryParam: {
        name: '',
      },
      list: [],
      selectedRowKeys: [],

      btnLoading: false,
      visible: false,
      form: {
        name: '',
        level: '',
        profit: '',
        price: '',
        status: '1',
      },
      rules: {
        name: [{ required: true, message: '等级名称不能为空', trigger: 'blur' }],
        level: [{ required: true, message: '会员等级不能为空', trigger: 'change' }],
        price: [{ required: true, message: '等级条件不能为空', trigger: 'blur' }],
        profit: [{ required: true, message: '折扣不能为空', trigger: 'blur' }],
        status: [{ required: true, message: '请选择', trigger: 'change' }],
      },
    }
  },

  mounted() {
    this.getList()
  },

  methods: {
    closeModal() {
      this.$refs['ruleForm'].resetFields()
    },

    async getList(pageNo = 1) {
      const hide = this.$message.loading('加载中', 0)
      let { queryParam, pageSize } = this

      const res = await userLevelSearch({
        ...queryParam,
        pageSize,
        pageNo,
      })

      if (res.code == 0) {
        this.pageNo = pageNo
        this.selectedRowKeys = []
        let { totalCount, list } = res.data
        this.total = totalCount
        this.list = list
      }

      hide()
    },

    // 分页请求
    tablePageChange(e) {
      this.getList(e)
    },

    // 点击单个
    handleItem(type, item, parmas) {
      switch (type) {
        case 'status':
          this.status({
            id: item.id,
            status: parmas,
          })
          break

        case 'edit':
          let { id, name, level, profit, price, status } = item
          this.form = {
            id,
            name,
            level,
            profit,
            price,
            status: String(status),
          }
          this.visible = true
          break
      }
    },

    async status(data) {
      const hide = this.$message.loading('请稍等...')
      const res = await setUserLevelStatus(data)
      hide()
      if (res.code) {
        this.$error({
          content: res.msg,
        })
        return
      }
      this.$message.success(res.msg)
      this.getList()
    },

    submit() {
      this.$refs['ruleForm'].validate(async (valid) => {
        if (valid) {
          let form = JSON.parse(JSON.stringify(this.form))

          this.btnLoading = true
          let res = await setUserLevel(form)
          if (res.code == 0) {
            this.$message.success('操作成功')
            this.visible = false
            this.getList()
          } else {
            this.$message.error(res.msg)
          }
          this.btnLoading = false
        } else {
          return false
        }
      })
    },
  },
}
</script>

<style lang="less" scoped>
.searchBox {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  padding: 8px 0 18px 0;
  /deep/ .ant-input-affix-wrapper {
    width: 220px;
    margin-right: 20px;
  }
  .select {
    display: flex;
    align-items: center;
    margin-right: 20px;
  }
}

.flex {
  .inp {
    margin-right: 16px;
  }
}

.flexBox {
  display: flex;
  align-items: center;
  border: 1px solid #d9d9d9;
  ::v-deep {
    .ant-input-number-input,
    .ant-input-number {
      border: 0;
      flex: 1;
      overflow: hidden;
      outline: none;
    }

    input {
      outline: none;
    }
  }

  em {
    padding: 0 12px;
    font-style: normal;
  }

  span {
    padding: 0 12px;
  }
}
</style>
